package com.ruoyi.carbon.service.impl;

import com.ruoyi.carbon.domain.Wuliao;
import com.ruoyi.carbon.domain.XiaoshouDingdan;
import com.ruoyi.carbon.domain.XiaoshouDingdanWuliao;
import com.ruoyi.carbon.domain.XiaoshouUser;
import com.ruoyi.carbon.domain.dto.XiaoshouDingdanDto;
import com.ruoyi.carbon.domain.vo.OrderDetail;
import com.ruoyi.carbon.domain.vo.OrderVo;
import com.ruoyi.carbon.domain.vo.XiaoshouDingdanVo;
import com.ruoyi.carbon.mapper.WuliaoMapper;
import com.ruoyi.carbon.mapper.XiaoshouDingdanMapper;
import com.ruoyi.carbon.mapper.XiaoshouDingdanWuliaoMapper;
import com.ruoyi.carbon.mapper.XiaoshouUserMapper;
import com.ruoyi.carbon.service.XiaoshouDingdanService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class XiaoshouDingdanServiceImpl implements XiaoshouDingdanService {

    @Autowired
    private XiaoshouDingdanMapper xiaoshouDingdanMapper;

    @Autowired
    private XiaoshouDingdanWuliaoMapper xiaoshouDingdanWuliaoMapper;

    @Autowired
    private XiaoshouUserMapper xiaoshouUserMapper;

    @Autowired
    private WuliaoMapper wuliaoMapper;


    @Override
    public List<XiaoshouDingdan> list(XiaoshouDingdan xiaoshouDingdan) {

        return xiaoshouDingdanMapper.list(xiaoshouDingdan);
    }

    @Override
    @Transactional
    public int saveOrUpdate(XiaoshouDingdanDto xiaoshouDingdanDto) {

        if (ObjectUtils.isEmpty(xiaoshouDingdanDto)) {
            throw new ServiceException("请输入要添加的数据");
        }


        XiaoshouDingdan xiaoshouDingdan = new XiaoshouDingdan();
        BeanUtils.copyProperties(xiaoshouDingdanDto, xiaoshouDingdan);
        // 金额合计
        xiaoshouDingdan.setDingdanNumber(UUID.randomUUID().toString());

        // 获取选择的所有物料
        List<XiaoshouDingdanWuliao> xiaoshouDingdanWuliaos = xiaoshouDingdanDto.getXiaoshouDingdanWuliaos();

        Double moneyTotal = 0.0;

        if (ObjectUtils.isNotEmpty(xiaoshouDingdanWuliaos)) {

            for (XiaoshouDingdanWuliao item : xiaoshouDingdanWuliaos) {

                Long dinghuoCount = Long.valueOf(item.getDinghuoCount());
                Double money = item.getMoney();
                if (dinghuoCount != null && money != null) {

                    moneyTotal = dinghuoCount * money;
                    moneyTotal = Math.ceil(moneyTotal);

                }
            }

        }
        xiaoshouDingdan.setMoneyTotal(String.valueOf(moneyTotal));


        // 获取当前登录用户名
        String username = SecurityUtils.getUsername();
        xiaoshouDingdan.setShenheRen(username);
        xiaoshouDingdan.setShenheRen(username);
        xiaoshouDingdan.setShenheStatus("1");

        Long id = xiaoshouDingdanDto.getId();

        // 新建或修改销售订单
        int result1 = 0;
        if (id == null) {

            result1 = xiaoshouDingdanMapper.save(xiaoshouDingdan);
        } else {
            result1 = xiaoshouDingdanMapper.update(xiaoshouDingdan);
        }
        if (result1 == 0) {
            throw new ServiceException("新建或更新失败失败");
        }

        // 处理销售订单物料
        // 删除所有的物料，方便修改
        xiaoshouDingdanWuliaoMapper.deleteByDingdanId(new Long[]{id});

        // 获取订单物料的数据
        if (ObjectUtils.isEmpty(xiaoshouDingdanWuliaos)) {
            // 没有物料信息不需要添加
            return result1;
        }
       xiaoshouDingdanWuliaos = xiaoshouDingdanWuliaos.stream().map(item -> {
           item.setId(null);
           item.setXiaoshouDingdanId(id);
           return item;
       }).collect(Collectors.toList());


        // 插入数据
        int result2 = xiaoshouDingdanWuliaoMapper.batchSave(xiaoshouDingdanWuliaos);

        if (result1 != 0 && result2 != 0) {
            return 1;
        } else {

            return 0;
        }
    }

    @Override
    @Transactional
    public int delete(Long[] ids) {

        xiaoshouDingdanWuliaoMapper.deleteByDingdanId(ids);
        return xiaoshouDingdanMapper.delete(ids);

    }

    @Override
    public XiaoshouDingdan getById(Long id) {

        return list(new XiaoshouDingdan(id)).get(0);
    }

    @Override
    public XiaoshouDingdanVo getDetailById(Long id) {

        // 查出客户名称
        XiaoshouDingdan xiaoshouDingdan = xiaoshouDingdanMapper.getById(id);
        Long xiaoshouUserId = Long.valueOf(xiaoshouDingdan.getXiaoshouUserId());
        XiaoshouUser xiaoshouUser = xiaoshouUserMapper.getById(id);
        String kehuName = xiaoshouUser.getKehuName();

        XiaoshouDingdanVo xiaoshouDingdanVo = new XiaoshouDingdanVo();
        xiaoshouDingdanVo.setKehuName(kehuName);

        List<OrderDetail> orderDetails = xiaoshouDingdanWuliaoMapper.getDetail(id);
        xiaoshouDingdanVo.setOrderDetails(orderDetails);
        return xiaoshouDingdanVo;
    }

    @Override
    public OrderVo getOrderVo(Long id) {

        // 查出客户名称
        XiaoshouDingdan xiaoshouDingdan = xiaoshouDingdanMapper.getById(id);
        Long xiaoshouUserId = Long.valueOf(xiaoshouDingdan.getXiaoshouUserId());
        XiaoshouUser xiaoshouUser = xiaoshouUserMapper.getById(xiaoshouUserId);

        List<XiaoshouDingdanWuliao> xiaoshouDingdanWuliaos = xiaoshouDingdanWuliaoMapper.getByXxddId(id);

        List<Wuliao> wuliaoList = new ArrayList<>();

        if (ObjectUtils.isNotEmpty(xiaoshouDingdanWuliaos)) {

            xiaoshouDingdanWuliaos.forEach(item -> {
                Long wuliaoId = item.getWuliaoId();
                List<Wuliao> wuliaos = wuliaoMapper.getList(wuliaoId);

                if (ObjectUtils.isNotEmpty(wuliaos)) {
                    wuliaoList.addAll(wuliaos);
                }
            });
        }

        OrderVo orderVo = new OrderVo();
        orderVo.setXiaoshouDingdanWuliaos(xiaoshouDingdanWuliaos);
        orderVo.setUser(xiaoshouUser);
        orderVo.setWuliaos(wuliaoList);


        return orderVo;
    }


}
